﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoMapper;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Web.Areas.Administration.Models.SellerAccount;
using AutoPartsSearch.Web.ViewModels;
using System.Web.Security;

namespace AutoPartsSearch.Web.Areas.Administration.Controllers
{
    public class SellerAccountsController : BaseAdminController
    {
        private ISellerRepository _sellerRepository;
        private IBlackListRepository _blackListRepository;

        public SellerAccountsController(ISellerRepository sellerRepository, IBlackListRepository blackListRepository)
        {
            this._sellerRepository = sellerRepository;
            this._blackListRepository = blackListRepository;
        }

        public ViewResult List(int page = 1, int pageSize = 50)
        {
            var sellers = this._sellerRepository.GetExtendedSellers(page, pageSize).ToList();
            var result = new PagedViewModel
                             {
                                 Sellers = sellers,
                                 PagingInfo = new PagingInfo()
                                                  {
                                                      CurrentPage = page,
                                                      ItemsPerPage = pageSize,
                                                      TotalItems = _sellerRepository.GetTotalCount()
                                                  }
                             };
            return View(result);
        }

        public ActionResult Details(int id)
        {
            //var culture = new CultureInfo("en-GB");
            //Thread.CurrentThread.CurrentUICulture = culture;
            //Thread.CurrentThread.CurrentCulture = culture;
            var result = this._sellerRepository.GetById(id);
            var viewModel = Mapper.Map<ViewModel>(result);
            viewModel.BlackListCount = _blackListRepository.GetBySellerCode(result.Code, false).Count();
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Block(int id, bool block)
        {
            var result = this._sellerRepository.GetById(id);
            result.IsBlocked = block;
            this._sellerRepository.Block(result.Id, block);
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult Quote(int id, bool enable)
        {
            var result = this._sellerRepository.GetById(id);
            result.IsQuoteEnabled = enable;
            this._sellerRepository.Quote(result.Id, enable);
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult UpdateExpiredDate(int id, string Expired)
        {
            var date = DateTime.ParseExact(Expired, "dd/MM/yyyy", Thread.CurrentThread.CurrentCulture);
            this._sellerRepository.UpdateExpiredDate(id, date);
            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var seller = this._sellerRepository.GetById(id);
            Membership.DeleteUser(seller.CompanyName, true);
            this._sellerRepository.Delete(seller.Id);
            return RedirectToAction("List");
        }
    }
}
